package com.lupan.passwordbox.controller;

import com.lupan.passwordbox.common.base.Response;
import com.lupan.passwordbox.domain.Person;
import com.lupan.passwordbox.service.PasswordBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 说明：
 *
 * @author lupan
 * @version 2017/8/11 13:22
 */
@RestController
@RequestMapping("pb")
public class PasswordBoxController {

    @Autowired
    private PasswordBoxService passwordBoxService;

    @RequestMapping(value = "get",method = RequestMethod.GET)
    public Response get(){
        Response response = Response.create("data");
        return response;
    }

    @RequestMapping(value = "person",method = RequestMethod.POST)
    public Response person(@RequestBody @Validated Person person,BindingResult bindingResult){
        Response response = Response.create(person);
        if(bindingResult.hasErrors()){
            List<ObjectError> errors = bindingResult.getAllErrors();
            for(ObjectError error : errors){
                System.out.println(error.getDefaultMessage());
            }
        }
        passwordBoxService.person(person);
        return response;
    }

    @RequestMapping(value = "personFormat",method = RequestMethod.GET)
    public Response personFormat(Person person){

        return Response.create(person);
    }
}
